package com.enqualcomm.kids.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.bean.StringMessage;
import com.enqualcomm.kids.component.MyApplication;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.UserDefault;
import com.enqualcomm.kids.view.gesturelock.GestureLockView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import common.utils.BufferedSharedPreferences;
import common.utils.MyLogger;
import common.utils.PromptUtil;
import de.greenrobot.event.EventBus;

/**
 * Created by Sunzhigang on 2016/3/24.
 */
@EActivity(R.layout.activity_gesture_lock_login)
public class GestureLockLoginActivity extends BaseActivity {
    private AppDefault appDefault;
    private UserDefault userDefault;

    private int tryTimes = 0;
    @ViewById(R.id.gesture_glv)
    GestureLockView gesture_glv;
    @ViewById(R.id.try_times_tv)
    TextView try_times_tv;
    @ViewById(R.id.input_error_gesture_lock_ll)
    LinearLayout input_error_gesture_lock_ll;

    @AfterViews
    void setupView() {
        appDefault = new AppDefault();
        userDefault = new UserDefault(appDefault.getUserid());
        gesture_glv.setOnGestureFinishListener(new GestureLockView.OnGestureFinishListener() {
            @Override
            public void OnGestureFinish(boolean success, String key) {
                if (!key.isEmpty())
                    checkGestureKey(key);
            }
        });
    }

    private void checkGestureKey(String key) {
        int lastTimes;
        if (key.equals(userDefault.getGestureKey())) {
            gesture_glv.setResult(true);
            startActivity(MainActivity_.class);
            finish();
        } else {
            input_error_gesture_lock_ll.setVisibility(View.VISIBLE);
            gesture_glv.setResult(false);
            TranslateAnimation animation = new TranslateAnimation(0, -5, 0, 0);
            animation.setInterpolator(new OvershootInterpolator());
            animation.setDuration(10);
            animation.setRepeatCount(5);
            animation.setRepeatMode(Animation.REVERSE);
            tryTimes++;
            lastTimes = 5 - tryTimes;
            try_times_tv.setText("密码错误，还可以再试" + lastTimes + "次");
            try_times_tv.startAnimation(animation);
            if (lastTimes == 0) {
                reLogin();
            }
        }
    }

    @Click(R.id.reLogin_tv)
    void reLoginByPassword() {
        reLogin();
    }

    private void reLogin() {
        /**清楚图形锁的设置*/
        userDefault.setStartOpenGestureLock(false);
        finish();
        EventBus.getDefault().post(new StringMessage(StringMessage.EXIT));
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            long l = SystemClock.elapsedRealtime();
            if ((l - exitTime) > 2000) {
                PromptUtil.toast(getApplicationContext(), R.string.press_to_exit);
                exitTime = l;
            } else {
                Intent intent = new Intent(this,MainActivity_.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("exitapp",true);
                startActivity(intent);
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
